package org.apache.camel.component.cmis;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;

import org.apache.camel.Converter;
import org.apache.camel.Exchange;
import org.apache.camel.util.MessageHelper;
import org.apache.chemistry.opencmis.commons.data.ContentStream;
import org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A helper class to transform Object into CMIS implementations
 */
@Converter
public class CmisConverter {
	
	private static final transient Logger	LOG	= LoggerFactory.getLogger(CmisConverter.class);
	
	@Converter
	public ContentStream toContentStream(InputStream stream, Exchange exchange) throws IOException {
		if (exchange != null && stream != null) {
			BigInteger contentLenght = exchange.getProperty(Exchange.CONTENT_LENGTH, BigInteger.class);
			String mimeType = exchange.getProperty(Exchange.CONTENT_TYPE, String.class);
			if (mimeType == null)
				mimeType = MessageHelper.getContentType(exchange.getIn());
			
			String fileName = exchange.getProperty(Exchange.FILE_NAME, String.class);
			LOG.debug("contentLenght : {}", contentLenght);
			LOG.debug("mimeType : {}", mimeType);
			LOG.debug("fileName : {}", fileName);
			return new ContentStreamImpl(fileName, contentLenght, mimeType, stream);
		}
		return null;
	}
}
